import 'package:flutter/foundation.dart';
import 'package:flutter_model_package/Course.dart';
import 'package:flutter_model_package/CourseCategory.dart';
import 'package:flutter_model_package/CourseLesson.dart';
import 'package:flutter_model_package/parse_base.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

class CourseClass extends MSParseBase {
  CourseClass() : super("CourseClass");

  CourseClass.cloe() : this();

  @override
  CourseClass clone(Map<String, dynamic> map) =>
      CourseClass.cloe()..fromJson(map);

  QueryBuilder<CourseClass> queryCourse() {
    return QueryBuilder(this)
      ..includeObject(['course', 'course.category', 'branch']);
  }

  /// 通过标签获得课程
  static Future<List<List<CourseClass>>> getTaggedCourse(
      List<String> tags) async {
    return Future.wait(tags.map((e) async {
      var query = QueryBuilder(CourseClass())
        ..includeObject(['course', 'course.category', 'branch'])
        ..whereContains("tags", e);
      var response = await query.query();
      if (response.results != null && response.results.isNotEmpty) {
        return response.results.map((e) => e as CourseClass).toList();
      }
      return List.empty();
    }));
  }

  /// 通过课程分类名称获得课程信息
  static Future<List<CourseClass>> getForCategory(
      CourseCategory category) async {
    var query = QueryBuilder(CourseClass())
      ..whereMatchesQuery("course", Course().getForCategory(category))
      ..includeObject(['course', 'course.category', 'branch']);
    var rep = await query.query();
    if (rep.results.isNotEmpty && rep.success) {
      return rep.results.map((e) => e as CourseClass).toList();
    }
    return List.empty();
  }

  QueryBuilder<CourseClass> queryByobjectId(String objectId) {
    return QueryBuilder(this)
      ..includeObject(['course', 'course.category', 'branch.contacts'])
      ..whereEqualTo("objectId", objectId);
  }

  QueryBuilder<CourseLesson> lessons() {
    return QueryBuilder(CourseLesson())..whereEqualTo("class", this);
  }
}
